package com.example.myapplication.Activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.myapplication.MainFragment.FragmentHome;
import com.example.myapplication.MainFragment.FragmentKeCheng;
import com.example.myapplication.MainFragment.FragmentMine;
import com.example.myapplication.MainFragment.FragmentTiKu;
import com.example.myapplication.R;

public class MainActivity extends AppCompatActivity {
    RadioGroup ShuoYe_rg;
    RadioButton ShuoYe_rb1,ShuoYe_rb2,ShuoYe_rb3,ShuoYe_rb4;
    //Fragment管理类
    FragmentManager ShuoYe_fm;
    //Fragme事务类
    FragmentTransaction fragmentTransaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化Fragment管理类
        ShuoYe_fm=getSupportFragmentManager();
        //开启事务
        fragmentTransaction=ShuoYe_fm.beginTransaction();
        //事务处理
        fragmentTransaction.replace(R.id.ShuoYe_fl,new FragmentHome());
        //提交事务
        fragmentTransaction.commit();
        initView();
    }

    private void initView() {
        ShuoYe_rg=findViewById(R.id.ShuoYe_rg);
        ShuoYe_rb1=findViewById(R.id.Shuoye_rb1);
        ShuoYe_rb2=findViewById(R.id.Shuoye_rb2);
        ShuoYe_rb3=findViewById(R.id.Shuoye_rb3);
        ShuoYe_rb4=findViewById(R.id.Shuoye_rb4);
        //添加单击事件
        ShuoYe_rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                //继续开启事务
                fragmentTransaction=ShuoYe_fm.beginTransaction();
                //设置全部未选中
                NoSelect();
                //判定选中状态
                switch (i){
                    case R.id.Shuoye_rb1:
                    ShuoYe_rb1.setCompoundDrawablesRelativeWithIntrinsicBounds(null,getResources().getDrawable(R.mipmap.home1),null,null);
                    ShuoYe_rb1.setTextColor(getResources().getColor(R.color.ShuoYe_rb_color));
                    fragmentTransaction.replace(R.id.ShuoYe_fl,new FragmentHome());
                        Log.i("1","1");
                    break;
                    case R.id.Shuoye_rb2:
                        ShuoYe_rb2.setCompoundDrawablesRelativeWithIntrinsicBounds(null,getResources().getDrawable(R.mipmap.quan1),null,null);
                        ShuoYe_rb2.setTextColor(getResources().getColor(R.color.ShuoYe_rb_color));
                        fragmentTransaction.replace(R.id.ShuoYe_fl,new FragmentTiKu());
                        Log.i("1","1");
                        break;
                    case R.id.Shuoye_rb3:
                        ShuoYe_rb3.setCompoundDrawablesRelativeWithIntrinsicBounds(null,getResources().getDrawable(R.mipmap.bofang1),null,null);
                        ShuoYe_rb3.setTextColor(getResources().getColor(R.color.ShuoYe_rb_color));
                        fragmentTransaction.replace(R.id.ShuoYe_fl,new FragmentKeCheng());
                        Log.i("1","1");
//                        Intent intent=new Intent();
//                        int num=intent.getIntExtra("num",1);
//                        FragmentKeCheng fragmentKeCheng=new FragmentKeCheng();
//                        FragmentTransaction ft=getSupportFragmentManager().beginTransaction();
//                        ft.replace(R.id.Home_fl,fragmentKeCheng);
//                        ft.commit();
                        break;
                    case R.id.Shuoye_rb4:
                        ShuoYe_rb4.setCompoundDrawablesRelativeWithIntrinsicBounds(null,getResources().getDrawable(R.mipmap.wode1),null,null);
                        ShuoYe_rb4.setTextColor(getResources().getColor(R.color.ShuoYe_rb_color));
                        fragmentTransaction.replace(R.id.ShuoYe_fl,new FragmentMine());
                        Log.i("1","1");
                        break;
                }
                fragmentTransaction.commit();
            }
        });


    }

    private void NoSelect() {
        ShuoYe_rb1.setCompoundDrawablesRelativeWithIntrinsicBounds(null,getResources().getDrawable(R.mipmap.home),null,null);
        ShuoYe_rb2.setCompoundDrawablesRelativeWithIntrinsicBounds(null,getResources().getDrawable(R.mipmap.quan),null,null);
        ShuoYe_rb3.setCompoundDrawablesRelativeWithIntrinsicBounds(null,getResources().getDrawable(R.mipmap.bofang),null,null);
        ShuoYe_rb4.setCompoundDrawablesRelativeWithIntrinsicBounds(null,getResources().getDrawable(R.mipmap.wode),null,null);
        ShuoYe_rb1.setTextColor(getResources().getColor(R.color.black));
        ShuoYe_rb2.setTextColor(getResources().getColor(R.color.black));
        ShuoYe_rb3.setTextColor(getResources().getColor(R.color.black));
        ShuoYe_rb4.setTextColor(getResources().getColor(R.color.black));
    }


}